<?php

/**
 * Atakan KOC Framework
 * -----------------------------------------------
 * Copyright (C) 2009 Atakan KOC
 * PHP versions 4 and 5
 * -----------------------------------------------
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * http://www.gnu.org/copyleft/gpl.html
 */
 
if ( !extension_loaded('mysql') && !defined('NO_DB') )
	trigger_error('Unable to load module for database server "mysql": PHP mysql extension not available!', E_USER_ERROR);

ini_set('mysql.trace_mode', '0');
	
class SQL_DataBase
{
  var $server      = 'localhost';
  var $user        = 'root';
  var $password    = '';
  var $dbase       = '';
  var $port        = '3306';
  var $querycount  = 0;
  var $queries     = array();
  var $connection  = 0;
  var $techmail    = '';
  var $errdesc     = '';
  var $errno       = 0;
  var $type        = 'MySQL';

	var $mysqli_functions = array(
		'affected_rows'      => 'mysqli_affected_rows',	
		'client_encoding'    => 'mysqli_client_encoding',
		'close'              => 'mysqli_close',
		'connect'            => 'mysqli_real_connect',
		'data_seek'          => 'mysqli_data_seek',
		'errno'              => 'mysqli_errno',
		'error'              => 'mysqli_error',
		'escape_string'      => 'mysqli_real_escape_string',
		'fetch_array'        => 'mysqli_fetch_array',
		'fetch_field'        => 'mysqli_fetch_field',
		'fetch_row'          => 'mysqli_fetch_row',
		'field_name'         => 'mysqli_field_tell',
		'free_result'        => 'mysqli_free_result',
		'insert_id'          => 'mysqli_insert_id',
		'num_fields'         => 'mysqli_num_fields',
		'num_rows'           => 'mysqli_num_rows',
		'pconnect'           => 'mysqli_real_connect', 
		'query'              => 'mysqli_query',
		'query_unbuffered'   => 'mysqli_unbuffered_query',
		'real_escape_string' => 'mysqli_real_escape_string',
		'select_db'          => 'mysqli_select_db',
	);

var $mysql_functions = array(
		'affected_rows'      => 'mysql_affected_rows',
		'client_encoding'    => 'mysql_client_encoding',
		'close'              => 'mysql_close',
		'connect'            => 'mysql_connect',
		'data_seek'          => 'mysql_data_seek',
		'errno'              => 'mysql_errno',
		'error'              => 'mysql_error',
		'escape_string'      => 'mysql_escape_string',
		'fetch_array'        => 'mysql_fetch_array',
		'fetch_field'        => 'mysql_fetch_field',
		'fetch_row'          => 'mysql_fetch_row',
		'field_name'         => 'mysql_field_name',
		'free_result'        => 'mysql_free_result',
		'insert_id'          => 'mysql_insert_id',
		'num_fields'         => 'mysql_num_fields',
		'num_rows'           => 'mysql_num_rows',
		'pconnect'           => 'mysql_pconnect',
		'query'              => 'mysql_query',
		'query_unbuffered'   => 'mysql_unbuffered_query',
		'real_escape_string' => 'mysql_real_escape_string',
		'select_db'          => 'mysql_select_db',    
  );
  var $functions = array();
  
  function SQL_DataBase($server,$dbase,$user,$password,$port='3306',$type='mysql',$techmail='') 
  {
    $this->server      = $server;
    $this->dbase       = $dbase;
    $this->user        = $user;
    $this->password    = $password;
    $this->techmail    = $techmail;
    
    if ($type='mysql')
    {
      $this->functions = $this->mysql_functions;
    }
    else
    {
      $this->functions = $this->mysqli_functions;
    }

    // Connect to server
		if ( version_compare(PHP_VERSION, '4.2.0', '>=') )
		  $this->connection = @$this->functions['connect']("$server:$port", $this->user, $this->password, true) or $this->errormsg("Connect failed");
		else
			$this->connection = @$this->functions['connect']("$server:$port", $this->user, $this->password) or $this->errormsg("Connect failed");

    // Select database
    $db_selected = @$this->functions['select_db']($this->dbase, $this->connection) or $this->errormsg("Cannot use database ".$this->dbase);    
  }

  function errormsg($msg)
  {
    $this->errdesc = $this->functions['error']();
    $this->errno   = $this->functions['errno']();
    
    $headers  = 'From: '.$this->techmail. "\r\n";
    $headers .= 'Reply-To: '. $this->techmail . "\r\n";
    $headers .= 'X-Mailer: PHP/' . phpversion(). "\r\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";

    $message .= $msg."\r\n";
    $message .= "Error: ".$this->errdesc."\r\n";
    $message .= "Error number: ".$this->errno."\r\n";
    $message .= "Date: ".gmdate("l dS of F Y h:i:s A"). "\r\n";
    $message .= "File: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']. "\r\n";
    $message .= "Referer :  " . $_SERVER['HTTP_REFERER'] . "\r\n";
    $message .= "Ip Address :  " . $_SERVER['REMOTE_ADDR'] . "\r\n";
    
    echo '<b>Database Error</b> <br /><p><form><textarea rows="15" cols="60">' . htmlspecialchars($message) . '</textarea></form></p>';

  
    if ($this->techmail)
    {
      @mail($this->techmail, "Database Error", $message, $headers);
    }
    exit;  
  }

  function query($sqlquery, $querylog=true) 
  {
    // Query Logs
    if ($querylog)
    {
      $this->queries[] = preg_replace('#\s+#', ' ', $query);
      $this->querycount++;
    }
  
    // Query Run
    $result = @$this->functions['query']($sqlquery, $this->connection);
  
    // Error Check
    if (!$result) 
    {
      $this->errormsg("Invalid query:" . $sqlquery);
    }
  
    return $result; 
  }

  function query_unbuffered($sqlquery, $querylog=true) 
  {
    // Query Logs
    if ($querylog)
    {
      $this->queries[] = preg_replace('#\s+#', ' ', $query);
      $this->querycount++;
    }
  
    // Query Run
    $result = @$this->functions['query_unbuffered']($sqlquery, $this->connection);
  
    // Error Check
    if (!$result) 
    {
      $this->errormsg("Invalid query:" . $sqlquery);
    }
  
    return $result; 
  }
    
  function query_first($sqlquery)
  {
    $queryid = $this->query($sqlquery);
    $returnarray = $this->functions['fetch_array']($queryid);
    $this->functions['free_result']($queryid);

    return $returnarray;
  }
  
	function fetch_array($queryresult, $type = MYSQL_ASSOC)
	{
		return @$this->functions['fetch_array']($queryresult, $type);
	}

  function free_result($queryid=-1)
  {
    return @$this->functions['free_result']($queryid);
  }
     
	function insert_id() 
  {
	   return @$this->functions['insert_id']($this->connection);
	}

  function fetch_row($queryresult)
  {
    return @$this->functions['fetch_row']($queryresult);
  }

	function fetch_assoc($queryresult)
	{
		return @$this->functions['fetch_assoc']($queryresult);
	}
    
	function num_rows($queryresult)
  {
		return $this->functions['num_rows']($queryresult);
	}
  
	function disconnect()
  {
		@$this->functions['close']($this->connection);
	}

  function num_fields($queryresult)
  {
  return @$this->functions['num_fields']($queryresult);
  }   

	function affected_rows()
	{
		return $this->functions['affected_rows']($this->connection);
	}

	function client_encoding()
	{
		return @$this->functions['client_encoding']($this->connection);
	}

	function close()
	{
		return @$this->functions['close']($this->connection);
	}

	function data_seek($queryresult, $index)
	{
		return @$this->functions['data_seek']($queryresult, $index);
	}

	function error()
	{
	 $this->errdesc = $this->functions['error']($this->connection);
	 return $this->errdesc;
	}

	function errno()
	{
		$this->errno = $this->functions['errno']($this->connection);
		return $this->errno;
	}

	function escape_string($string)
	{
		if(function_exists("mysql_real_escape_string"))
		{
			$string = $this->functions['real_escape_string']($string);
		}
		elseif(function_exists("mysql_escape_string"))
		{
			$string = $this->functions['escape_string']($string);
		}
		else
		{
			$string = addslashes($string);
		}
		return $string;
	}

	function fetch_field($queryresult)
	{
		return @$this->functions['fetch_field']($queryresult);
	}

	function field_name($queryresult, $index)
	{
		return @$this->functions['field_name']($queryresult, $index);
	}

	function select_db($database = '', $link = null)
	{
		return $this->functions['select_db']($database, $link);
	}
              	
}

?>
